<template>
	<view>
		<view class="head">
			<view class="head_left" @tap="back()">
				<image src="../../../static/wode/return.png" mode=""></image>
				<text>返回</text>
			</view>
			<view class="head_mid">
				好友列表
			</view>
			<view class="head_right">

			</view>
		</view>
		<view class="">
			<!-- <view class="" v-for="(item,index) in tatable_friends_data" :key="index">
				
			</view> -->
			<view class="friend_bottom">
				<view class="friend_bottom_group">
					#
					<!--  获取好友列表进行拼音字母排序后重新渲染 -->
				</view>
				<view class="padding-box" >
					<view class="frient_bottom_item" @tap="toChat(item.Info_Account,item.SessionType,item.head,item.Nick)" v-for="(item,index) in tatable_friends_data"
				 :key="index">
					<!-- <view class="image_box">
						<image class="headImg" src="../../../static/avatar.jpg" mode=""></image>
						<image class="country" src="../../../static/meiguo.png" mode=""></image>
					</view> -->

					<image :src="item.head" mode=""></image>
					<text class="text1">{{item.Nick}}</text>
					<!-- <text class="text2">{{item.Info_Account}}</text> -->
					<!-- <view class="bottomborder"> -->
					</view>
				</view>
				
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import webim from '../../../common/webim_wx.min.js' //腾讯云im包

	export default {
		data() {
			return {
				id: '',
				tatable_friends_data: [],
				type:'',
			}
		},
		onLoad(option) {
			if(option.type!=undefined&&option.type!=""){
				this.type=option.type
			}
		},
		onShow() {
			if (uni.getStorageSync('user_id') != undefined || uni.getStorageSync('user_id') != '') {
				this.id = uni.getStorageSync('user_id')
			}
			this.getAllFriend()
		},
		methods: {
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			getAllFriend() { //获取我所有好友
				var options = {
					'From_Account': this.id,
					'TimeStamp': 0,
					'StartIndex': 0,
					'GetCount': 100,
					'LastStandardSequence': 0,
					"TagList": [
						"Tag_Profile_IM_Nick",
						"Tag_SNS_IM_Remark",
						"Tag_Profile_IM_Gender",
						"Tag_Profile_IM_Image",
						// "MsgShow"
					]
				};
				var _this = this
				webim.getAllFriend(
					options,
					function(resp) {
						console.log(resp)
						if (resp.FriendNum > 0) {
							var table_friends_data = [];
							var friends = resp.InfoItem;
							if (!friends || friends.length == 0) {
								uni.showToast({
									title: '你目前还没有好友',
									icon: 'none'
								})
								return;
							}
							var count = friends.length;
							//判断性别
							for (var i = 0; i < count; i++) {
								var friend_name = friends[i].Info_Account;
								var gender = "未知";
								if (friends[i].SnsProfileItem) {
									for (var j in friends[i].SnsProfileItem) {
										switch (friends[i].SnsProfileItem[j].Tag) {
											case 'Tag_Profile_IM_Nick':
												friend_name = friends[i].SnsProfileItem[j].Value;
												break;
											case 'Tag_Profile_IM_Gender':
												switch (friends[i].SnsProfileItem[j].Value) {
													case 'Gender_Type_Male':
														gender = '男';
														break;
													case 'Gender_Type_Female':
														gender = '女';
														break;
													case 'Gender_Type_Unknown':
														gender = '未知';
														break;
												}
												break;
										}
									}
								}
								table_friends_data.push({
									'Info_Account': friends[i].Info_Account,
									'Nick': webim.Tool.formatText2Html(friend_name),
									'head': friends[i].SnsProfileItem[2].Value,//这里添加会报错
									'Gender': gender,
									'SessionType': 'C2C'
								});
							}
							//判断性别
							console.log(table_friends_data)
							_this.tatable_friends_data = table_friends_data
							// 				            $('#get_my_friend_table').bootstrapTable('load', table_friends_data);
							// 				            $('#get_my_friend_dialog').modal('show');
						} else {
							uni.showToast({
								title: '您目前还没有好友',
								icon: 'none'
							})
						}

					},
					function(err) {
						console.log(err);
					}
				);
			},
			FriendList() {
				this.tatable_friends_data = {

				}
			},
			toChat(item, chatType,head,name) { //聊天
			if(this.type=='card'){
				var cardData={
					type:'card',
					userId:item,
					userIcon:head,
					userNickName:name
				}
				uni.setStorageSync('cardData',cardData)
				uni.navigateBack({
					delta:1
				})
				return
			}
				this.selToID = item
				console.log(item, chatType)
				uni.navigateTo({
					url: "/pages/Friend/chat/chat?item=" + item + "&chatType=" + chatType + "&MsgSeq=-1"
				})
			},
			sendC2cMsg(){
			
			    toAccount=$("#scm_to_account").val();
			    msgtosend=$("#scm_content").val();
			
			    var msgLen = webim.Tool.getStrBytes(msgtosend);
			
			    var maxLen, errInfo;
			
			    maxLen = webim.MSG_MAX_LENGTH.C2C;
			    errInfo = "消息长度超出限制(最多" + Math.round(maxLen / 3) + "汉字)";
			
			    if (msgtosend.length < 1) {
			        alert("发送的消息不能为空!123");
			        $("#send_msg_text").val('');
			        return;
			    }
			
			    if (msgLen > maxLen) {
			        alert(errInfo);
			        return;
			    }
			
			    var sess=webim.MsgStore.sessByTypeId(webim.SESSION_TYPE.C2C, toAccount);
			    if (!sess) {
			        sess = new webim.Session(webim.SESSION_TYPE.C2C, toAccount, toAccount, friendHeadUrl, Math.round(new Date().getTime() / 1000));
			    }
			    var isSend = true;//是否为自己发送
			    var seq = -1;//消息序列，-1表示sdk自动生成，用于去重
			    var random = Math.round(Math.random() * 4294967296);//消息随机数，用于去重
			    var msgTime = Math.round(new Date().getTime() / 1000);//消息时间戳
			    var subType;//消息子类型
			
			    subType = webim.C2C_MSG_SUB_TYPE.COMMON;
			
			    var msg = new webim.Msg(sess, isSend, seq, random, msgTime, loginInfo.identifier, subType, loginInfo.identifierNick);
			
			    var text_obj;
			
			    text_obj = new webim.Msg.Elem.Text(msgtosend);
			    msg.addText(text_obj);
			
			    webim.sendMsg(msg, function (resp) {
			
			
			        if (!selToID) {//没有聊天会话
			            selType=webim.SESSION_TYPE.C2C;
			            selToID=toAccount;
			            selSess=sess;
			            addSess(selType,toAccount, toAccount, friendHeadUrl, 0, 'sesslist');
			            setSelSessStyleOn(toAccount);
			            //私聊时，在聊天窗口手动添加一条发的消息，群聊时，长轮询接口会返回自己发的消息
			            addMsg(msg);
			        }else{//有聊天会话
			            if(selToID==toAccount){//聊天对象不变
			                addMsg(msg);
			            }else{//聊天对象发生改变
			                var tempSessDiv = document.getElementById("sessDiv_" + toAccount);
			                if(!tempSessDiv){//不存在这个会话
			                    addSess(webim.SESSION_TYPE.C2C,toAccount, toAccount, friendHeadUrl, 0, 'sesslist');//增加一个会话
			                }
			
			                onSelSess(webim.SESSION_TYPE.C2C,toAccount);//再进行切换
			            }
			        }
			        webim.Tool.setCookie("tmpmsg_" + toAccount, '', 0);
			        $("#scm_content").val('');
			        $('#send_c2c_msg_dialog').modal('hide');
			        $('#get_my_friend_dialog').modal('hide');
			
			    }, function (err) {
			        alert(err.ErrorInfo);
			        $("#scm_content").val('');
			    });
			}
		}
	}
</script>

<style scoped>
	.head {
		width: 100%;
		display: flex;
		justify-content: space-between;
		height: 128upx;
		background: #ffffff;
		padding: 0 24upx;
		z-index: 200;
		position: fixed;
		top: 0;
	}

	.head image {
		width: 20upx;
		height: 36upx;
		margin-right: 9upx;
	}

	.head_left {
		height: 82upx;
		width: 106upx;
		display: flex;
		align-items: center;
		line-height: 82upx;
		color: #666666;
		font-size: 32upx;

	}

	.head_right {
		width: 106upx;
	}
	.padding-box{
		padding-top: 97upx;
	}

	.frient_bottom_item {
		background: #FFFFFF;
		display: flex;
		/* align-items: center; */
		padding: 20upx 24upx;
		position: relative;
		border-bottom: 1upx solid #E6E6E6;
	}

	.friend_bottom_group {
		padding: 10upx 24upx;
		font-size: 24upx;
		color: #999999;
	}

	.frient_bottom_item image {
		width: 80upx;
		height: 80upx;
		border-radius: 50%;
		overflow: hidden;
		margin-right: 20upx;
	}

	.text1 {
		color: #333333;
		font-size: 32upx;
		/* padding-bottom: 22upx; */
	}

	.text2 {
		color: #999999;
		font-size: 26upx;
	}
</style>
